<?php

declare(strict_types=1);

namespace App\CustHub\Mapper;

use App\CustHub\Model\ChAffiliatePid;
use Hyperf\Config\Annotation\Value;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;

class ChAffiliatePidMapper extends AbstractMapper
{

    #[Value('cache.default.prefix')]
    protected string $prefix;

    public function assignModel()
    {
        $this->model = ChAffiliatePid::class;
    }

    public function findByAffiliateAccountId(mixed $affiliateAccountId): array|\Hyperf\Database\Model\Collection
    {
        return $this->model::query()->where('affiliate_account_id', $affiliateAccountId)->get();
    }


    public function rmCache(mixed $id): void
    {
        $redis = redis();
        $keys[] = $this->prefix . 'affiliate_pid_info:'.$id;
        $redis->del($keys);
    }


    public function handleSearch(Builder $query, array $params): Builder
    {
        if (isset($params['affiliate_account_id']) && $params['affiliate_account_id']) {
            $query->where('affiliate_account_id', $params['affiliate_account_id']);
        }
        return $query;
    }

    public function handleOrder(Builder $query, ?array &$params = null): Builder
    {
        return $query->orderByDesc('created_at');
    }
}